package com.open.demo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.open.demo.R;
import com.open.demo.bean.DangBeiAppInfo;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 当呗APP信息Adpater.
 */
public class AppInfoAdapter extends BaseAdapter {

    private List<DangBeiAppInfo.AppInfo> mAppInfoList;
    private LayoutInflater mInflater;

    public AppInfoAdapter(Context context, List<DangBeiAppInfo.AppInfo> appInfoList) {
        this.mAppInfoList = appInfoList;
        this.mInflater = LayoutInflater.from(context);
    }

    public void setData(List<DangBeiAppInfo.AppInfo> appInfoList) {
        this.mAppInfoList = appInfoList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mAppInfoList == null) return 0;
        return mAppInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mAppInfoList == null) return position;
        return mAppInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        DangBeiAppInfo.AppInfo appinfo = mAppInfoList.get(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_gridview, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 设置APP信息.
        if (appinfo != null) {
            holder.titleTv.setText(appinfo.getApptitle());
//            holder.appIv
//            String appIcoUrl = appinfo.getAppico();
        }
        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.app_iv)
        ImageView appIv;  // app图标.
        @InjectView(R.id.title_tv)
        TextView titleTv; // app标题.

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
